package com.kd.juhaowan.ui.holder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.kd.juhaowan.R;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.HomeResult;
import com.kd.juhaowan.ui.adapter.BannerPagerAdapter;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.view.ScollTextView;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

import butterknife.BindView;

public class HomeHeadViewHolder extends BaseViewHolder implements OnBannerListener {

    private static final float BANNER_ASPECT_RATIO = 1f / 2.5f;

    @BindView(R.id.mall_home_banner)
    Banner<String, BannerPagerAdapter> bannerView;

    @BindView(R.id.fragment_home_scrollTextView)
    ScollTextView mScrollTextView;

    @BindView(R.id.home_newgame_list)
    RecyclerView newGameListView;

    private BaseRecyclerViewAdapter<HotGameViewHolder, GameModel> mAdapter;


    public HomeHeadViewHolder(@NonNull View itemView) {
        super(itemView);
        bannerView.setBannerRound2(UIUtil.dip2px(getContext(), 5));
        bannerView.setIndicator(new CircleIndicator(getContext()));
        initAdapter();
    }


    private void initAdapter(){
        mAdapter = new BaseRecyclerViewAdapter<HotGameViewHolder, GameModel>() {
            @NonNull
            @Override
            public HotGameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_home_newgame, parent, false);
                HotGameViewHolder holder=new HotGameViewHolder(view);
//                holder.setOnItemClickListener();
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull HotGameViewHolder holder, int position) {
                holder.setDate(getItem(position));

            }
        };

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {

            }
        });
        mAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);

        newGameListView.setLayoutManager(layoutManager);
        newGameListView.setAdapter(mAdapter);
    }


    private List<GameModel> bannerModels;
//    public void setDate(List<HomeBannerModel> models){
//        this.models = models;
//
//       /* final int screenWidth = UIUtil.getScreenWidth(getContext());
//        ViewGroup.LayoutParams layoutParams = bannerView.getLayoutParams();
//        layoutParams.height = (int) (screenWidth * BANNER_ASPECT_RATIO);
//        bannerView.setLayoutParams(layoutParams);*/
//
//        BannerPagerAdapter bannerAdapter = new BannerPagerAdapter(models);
//        bannerView.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
//        bannerView.setAdapter(bannerAdapter);
//        bannerView.setOnBannerListener(this);
//        titleView.setVisibility(View.GONE);
//    }

    public void setGameDate(HomeResult models){
        bannerModels = models.getHot_game();
        BannerPagerAdapter bannerAdapter = new BannerPagerAdapter(bannerModels);
        bannerView.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
        bannerView.setAdapter(bannerAdapter);
        bannerView.setOnBannerListener(this);


        mScrollTextView.setData(models.getNotice_list());
        mAdapter.resetAdapterData(models.getNewGameList());
//        this.models = models;

       /* final int screenWidth = UIUtil.getScreenWidth(getContext());
        ViewGroup.LayoutParams layoutParams = bannerView.getLayoutParams();
        layoutParams.height = (int) (screenWidth * BANNER_ASPECT_RATIO);
        bannerView.setLayoutParams(layoutParams);*/

//        BannerPagerAdapter bannerAdapter = new BannerPagerAdapter(models);
//        bannerView.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
//        bannerView.setAdapter(bannerAdapter);
//        bannerView.setOnBannerListener(this);
//        titleView.setVisibility(View.VISIBLE);
    }


//    public void setGameDate(List<HomeBannerModel> models){
//        this.models = models;
//
//       /* final int screenWidth = UIUtil.getScreenWidth(getContext());
//        ViewGroup.LayoutParams layoutParams = bannerView.getLayoutParams();
//        layoutParams.height = (int) (screenWidth * BANNER_ASPECT_RATIO);
//        bannerView.setLayoutParams(layoutParams);*/
//
//        BannerPagerAdapter bannerAdapter = new BannerPagerAdapter(models);
//        bannerView.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
//        bannerView.setAdapter(bannerAdapter);
//        bannerView.setOnBannerListener(this);
//        titleView.setVisibility(View.VISIBLE);
//    }

    @Override
    public void OnBannerClick(Object data, int position) {
//        HomeBannerModel model = (HomeBannerModel)data;
//        if("1".equals(model.getType())){
//            Intent intent = new Intent(getContext(), WebActivity.class);
//            intent.putExtra("url",model.getUrl());
//            getContext().startActivity(intent);
//        }

//        try {
//            JSONObject jsonObj = new JSONObject(model.getJumpUrl());
//            String id = jsonObj.getJSONObject("param").getString("id");
//            Intent intent =new Intent(getContext(), GameInfoActivity.class);
//            intent.putExtra("id",id);
//            getContext().startActivity(intent);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

    }
}
